package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SysUserMapper;
import com.ktjiaoyu.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @return 13141
 * 2025/6/24
 * 14:46
 */
@Service("sysUserService")
// 如果在类上添加@Transactional注解，则对本类中所有业务方法都有效
//@Transactional
public class SysUserServiceImpl implements SysUserService {
    // 定义 Mapper
    @Resource
    private SysUserMapper sysUserMapper;
    // 提供 setter 方法用于注入属性
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
    // 查询用户列表
    @Override
    public List<SysUser> findList(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }

    // 根据供货商名模糊查询
    @Override
    // 若当前方法支持事务管理，则添加@Transactional注解
    @Transactional
    public int saveBatch(List<SysUser> sysUserList) {
        int rows = 0;
        for (int i = 0; i < sysUserList.size(); i++){
            // 拿出每一条用户数据插入
            SysUser sysUser = sysUserList.get(i);
            if (i == 1){
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响操作
            rows += sysUserMapper.insert(sysUser);
        }
        // 返回总受影响操作
        return rows;
    }

    // 删除用户
    @Override
    @Transactional
    public int deleteUser(Integer id) {
        return sysUserMapper.deleteUser(id);
    }

    // 修改用户
    @Override
    @Transactional
    public int updateUser(SysUser sysUser) {
        return sysUserMapper.updateUser(sysUser);
    }
}
